﻿#include "rfmmain.h"
#include "ui_rfmmain.h"

#include <QTimer>
#include <QtEndian>
#include <QMessageBox>
#include <QDebug>

#if (defined(WIN32))
#include "rfm2g_windows.h"
#endif

#include "./rfm2g_api.h"
#include "./rfmapi.h"

RfmMain::RfmMain(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::RfmMain)
    , m_readTimer(nullptr)
{
    ui->setupUi(this);

    this->setWindowTitle(QStringLiteral("反射内存读写工具"));

    this->initUI();
}

RfmMain::~RfmMain()
{
    delete ui;
}

void RfmMain::initUI()
{
    RfmApi::getInstance()->open();
}


void RfmMain::on_pushButton_read_clicked()
{
    quint32 addr = ui->spinBox_base_addr->value() + ui->spinBox_addr->value();
    QString value;
    int ret = RfmApi::getInstance()->read(addr, value, ui->spinBox_len->value());
    if(ret == 0)
    {
        ui->lineEdit_r10->setText(value);
        // QMessageBox::information(nullptr, QStringLiteral("提示消息"), QStringLiteral("读取成功~"));
        this->on_pushButton_r10_clicked();
        return;
    }
    QMessageBox::information(nullptr, QStringLiteral("提示消息"), QStringLiteral("读取失败~"));
}

void RfmMain::on_pushButton_write_clicked()
{
    QString value =  ui->lineEdit_w10->text();
    quint32 addr = ui->spinBox_base_addr->value() + ui->spinBox_addr->value();
    int num = ui->spinBox_num->value();

    QList<bool> retList;
    for (int index = 0; index < num; ++index)
    {
        addr = addr + ui->spinBox_len->value() * index;
        qDebug() << __LINE__ << __FUNCTION__ << "value= "  << addr;
        int ret = RfmApi::getInstance()->write(addr, value, ui->spinBox_len->value());
        retList.append(ret == 0);
    }

    if(!retList.contains(false))
    {
        QMessageBox::information(nullptr, QStringLiteral("提示消息"), QStringLiteral("设置成功~"));
        return;
    }

    QMessageBox::critical(nullptr, QStringLiteral("提示消息"), QStringLiteral("设置失败~"));
}

void RfmMain::on_pushButton_clear_clicked()
{
    quint32 addr = ui->spinBox_base_addr->value() + ui->spinBox_addr->value();
    int ret = RfmApi::getInstance()->write(addr, 0, ui->spinBox_len->value());
    if(ret == 0)
    {
        QMessageBox::information(nullptr, QStringLiteral("提示消息"), QStringLiteral("清理成功~"));
        return;
    }

    QMessageBox::critical(nullptr, QStringLiteral("提示消息"), QStringLiteral("清理失败~"));
}


void RfmMain::setWOtherEditData(const QString &arg1, int type)
{
    if(type == 2)
    {
        QString s10 = QString::number(arg1.toUInt(nullptr, 2), 10);
        QString s16 = QString::number(arg1.toUInt(nullptr, 2), 16);

        ui->lineEdit_w10->setText(s10);
        ui->lineEdit_w16->setText(s16);
    }
    else if(type == 10)
    {
        QString s2 = QString::number(arg1.toUInt(), 2);
        QString s16 = QString::number(arg1.toUInt(), 16);

        ui->lineEdit_w2->setText(s2);
        ui->lineEdit_w16->setText(s16);
    }
    else if(type == 16)
    {
        QString s2 = QString::number(arg1.toUInt(nullptr, 16), 2);
        QString s10 = QString::number(arg1.toUInt(nullptr, 16), 10);

        ui->lineEdit_w2->setText(s2);
        ui->lineEdit_w10->setText(s10);
    }
}

void RfmMain::setROtherEditData(const QString &arg1, int type)
{
    if(type == 2)
    {
        QString s10 = QString::number(arg1.toUInt(nullptr, 2), 10);
        QString s16 = QString::number(arg1.toUInt(nullptr, 2), 16);

        ui->lineEdit_r10->setText(s10);
        ui->lineEdit_r16->setText(s16);
    }
    else if(type == 10)
    {
        QString s2 = QString::number(arg1.toUInt(), 2);
        QString s16 = QString::number(arg1.toUInt(), 16);

        ui->lineEdit_r2->setText(s2);
        ui->lineEdit_r16->setText(s16);
    }
    else if(type == 16)
    {
        QString s2 = QString::number(arg1.toUInt(nullptr, 16), 2);
        QString s10 = QString::number(arg1.toUInt(nullptr, 16), 10);

        ui->lineEdit_r2->setText(s2);
        ui->lineEdit_r10->setText(s10);
    }
}

void RfmMain::on_pushButton_w10_clicked()
{
    setWOtherEditData(ui->lineEdit_w10->text(), 10);
}

void RfmMain::on_pushButton_w16_clicked()
{
    setWOtherEditData(ui->lineEdit_w16->text(), 16);
}

void RfmMain::on_pushButton_w2_clicked()
{
    setWOtherEditData(ui->lineEdit_w2->text(), 2);
}

void RfmMain::on_pushButton_r10_clicked()
{
    setROtherEditData(ui->lineEdit_r10->text(), 10);
}

void RfmMain::on_pushButton_r16_clicked()
{
    setROtherEditData(ui->lineEdit_r16->text(), 16);
}

void RfmMain::on_pushButton_r2_clicked()
{
    setROtherEditData(ui->lineEdit_r2->text(), 2);
}
